from django import forms
from django.core.exceptions import ValidationError

"""
注册用到的from类 start
"""


class RegForm(forms.Form):
    username = forms.CharField(
        max_length=10,
        label="用户名",
        error_messages={
            "max_length": "用户名最长为16位",
            "required": "用户名不能为空"
        },
        widget=forms.widgets.TextInput(
            attrs={"class": "form-control"}
        )
    )
    password = forms.CharField(
        min_length=6,
        max_length=16,
        label="密码",

        error_messages={
            "min_length": "密码最少6位",
            # "max_length": "密码最长为16位",
            "required": "密码不能为空",
        },
        widget=forms.widgets.PasswordInput(
            attrs={"class": "form-control"},
            # 刷新后密码仍保留
            render_value=True

        ),

    )
    re_password = forms.CharField(
        min_length=6,
        max_length=16,
        label="确认密码",

        error_messages={
            "min_length": "密码最少6位",
            "required": "密码不能为空",
            # "max_length": "密码最长为16位",
        },
        widget=forms.widgets.PasswordInput(
            attrs={"class": "form-control"},
            # 刷新后密码仍保留
            render_value=True

        ),
    )
    email = forms.EmailField(
        label="邮箱",

        error_messages={
            "invalid": "邮箱格式不正确!",
            "required": "邮箱不能为空",
        },
        widget=forms.widgets.EmailInput(
            attrs={"class": "form-control"}

        ),

    )

    # 重写全局钩子函数，对确认密码做校验
    # def clean(self):
    #     password = self.cleaned_data.get("password")
    #     re_password = self.cleaned_data.get("re_password")
    #     if re_password and re_password != password:
    #         self.add_error("re_password", ValidationError("两次密码不一致"))
    #     else:
    #         return self.cleaned_data

    # 重写clean_re_password钩子函数，对确认密码做校验
    def clean_re_password(self):
        password = self.cleaned_data.get("password")
        re_password = self.cleaned_data.get("re_password")
        if re_password and re_password != password:
            raise ValidationError("两次密码不一致")
        else:
            return self.cleaned_data


"""
注册用到的from类 ending
"""
"""
user_info 表 from类 start
"""

# class User_info(forms.Form):



"""
user_info 表 from类 ending
"""
